<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>蜗牛二手图书交易网首页</title>
    <!--引入资源：vue要在element之前引入-->
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.14/index.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.14/theme-chalk/index.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"></script>
    <style>
        .el-row {
            margin-bottom: 20px;

        &
        :last-child {
            margin-bottom: 0;
        }

        }
        .el-col {
            border-radius: 4px;
        }

        .bg-purple-dark {
            background: #99a9bf;
        }

        .bg-purple-light {
            background: #e5e9f2;
        }

        .grid-content {
            border-radius: 4px;
            min-height: 36px;
            display: flex;
            justify-content: center; /* 水平居中 */
            align-items: center; /* 垂直居中 */
        }
    </style>
</head>
<body>
<div id="app">
    <!--第一行：头部-->
    <el-row>
        <el-col :span="24">
            <div class="grid-content bg-purple-light">
                <el-link type="info" :href="'/index.html'">蜗牛学苑</el-link>
            </div>
        </el-col>
    </el-row>

    <el-row>
        <el-col :span="10" :offset="7">
            <!--model 绑定的对象； roles 校验规则，需要定义； ref 表单引用名称，id； -->
            <el-form :model="user" :rules="rules" ref="ruleForm" label-width="100px">
                <el-form-item label="用户账号" prop="account">
                    <el-input v-model="user.account"></el-input>
                </el-form-item>
                <el-form-item label="用户密码" prop="password">
                    <el-input type="password" v-model="user.password"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="user.checkPass"></el-input>
                </el-form-item>
                <el-form-item label="邮箱地址" prop="email">
                    <el-input v-model="user.email"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="valicode">
                    <el-input v-model="user.valicode" style="width:200px;"></el-input>
                    <el-button type="primary" @click="sendCode">发送验证码</el-button>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">注 册</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</div>
</body>
<script>
    new Vue({
        el: "#app",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    // 这里判断确认密码不等于空的时候，校验密码与确认密码的一致
                    if (this.user.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.user.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                user:{},
                rules:{
                    account: [
                        { required: true, message: '请输入账号', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { required: true, message: '请输入确认密码', trigger: 'blur' },
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                    ],
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post(`/user/register`,this.user).then(resp=>{
                            if (resp.data.code == 200) {
                                this.$message.success('注册成功，3秒后跳转到登录页面');
                                setTimeout(()=>{
                                    location.href = '/login.html';
                                },3000)
                            } else {
                                this.$message.error('注册失败');
                                return false;
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            sendCode(){
                axios.get(`/email/${this.user.email}`).then(resp=>{
                    this.$message.success('验证码已发送,请查收');
                })
            }
        }
    })
</script>
</html>